package com.mingliqiye.minecraft.enchantment.conflict.fabric;

import com.mingliqiye.minecraft.enchantment.conflict.common.ConfigPair;
import com.mingliqiye.minecraft.enchantment.conflict.common.MinecraftMod;
import com.mingliqiye.minecraft.enchantment.conflict.common.WebConfigCoreMeMethod;
import com.mingliqiye.minecraft.enchantment.conflict.common.enchantment.EnchantmentEntity;
import com.mingliqiye.minecraft.enchantment.conflict.fabric.network.ConfigPayload;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.minecraft.core.registries.BuiltInRegistries;

public class FabricWebConfigCoreMeMethod implements WebConfigCoreMeMethod {

    @Override
    public void reloadConfig() {}

    @Override
    public Map<String, ConfigPair> getDefConfig() {
        return FabricMod.CONFIGMANAGER.getDefConfig().getConfig();
    }

    @Override
    public Map<String, ConfigPair> getConfig() {
        return FabricMod.CONFIGMANAGER.getConfig();
    }

    @Override
    public String getGameVersions() {
        Optional<ModContainer> minecraftContainer = FabricLoader.getInstance().getModContainer("minecraft");
        if (minecraftContainer.isPresent()) {
            return minecraftContainer.get().getMetadata().getVersion().getFriendlyString();
        } else {
            return "未知版本";
        }
    }

    @Override
    public String getModVersions() {
        Optional<ModContainer> EDNCContainer = FabricLoader.getInstance().getModContainer(MinecraftMod.MOD_ID);
        if (EDNCContainer.isPresent()) {
            return EDNCContainer.get().getMetadata().getVersion().getFriendlyString();
        } else {
            return "未知版本";
        }
    }

    @Override
    public String getLoaderVersions() {
        Optional<ModContainer> loaderContainer = FabricLoader.getInstance().getModContainer("fabricloader");
        if (loaderContainer.isPresent()) {
            return loaderContainer.get().getMetadata().getVersion().getFriendlyString();
        } else {
            return "未知版本";
        }
    }

    @Override
    public String getLoaderName() {
        return "Fabric";
    }

    @Override
    public List<EnchantmentEntity> getAllEnchantments() {
        return BuiltInRegistries.ENCHANTMENT.stream()
            .map((a) ->
                new EnchantmentEntity(
                    a.getDescriptionId(),
                    "null",
                    a.getFullname(a.getMaxLevel()).getString(),
                    BuiltInRegistries.ENCHANTMENT.getId(a)
                )
            )
            .toList();
    }

    @Override
    public boolean getTest(Integer fid, Integer sid) {
        var f = BuiltInRegistries.ENCHANTMENT.byId(fid);
        var s = BuiltInRegistries.ENCHANTMENT.byId(sid);
        return f.isCompatibleWith(s) && s.isCompatibleWith(f);
    }

    @Override
    public void saveConfig(Map<String, ConfigPair> configPairMap) {
        FabricMod.CONFIGMANAGER.setConfig(configPairMap);
        FabricMod.CONFIGMANAGER.save();
        ConfigPayload.sendConfigToAllClient();
    }
}
